﻿//using System;
//using System.Collections.Generic;
//using System.Collections.Specialized;
//using System.Configuration;
//using System.Configuration.Provider;
//using System.Data.Services.Client;
//using System.Linq;
//using System.Web.Profile;
//using AzureTableStorage;
//using WebPages.Ccc;

//namespace BlogAPI.Library
//{
//    public class AzureProfileProvider : ProfileProvider
//    {
//        public struct PropertyNames
//        {
//            public const string Gender = "Gender";
//            public const string Birthdate = "Birthdate";
//            public const string PortraitBlobAddressUri = "PortraitBlobAddressUri";
//            public const string Tags = "Tags";
//        }

//        public override void Initialize(string name, NameValueCollection config)
//        {
//            // Initialize values from web.config.
//            if (config == null) throw new ArgumentNullException("config");

//            if (String.IsNullOrEmpty(name)) throw new ArgumentNullException("config");

//            // Initialize the abstract base class.
//            base.Initialize(name, config);

//            ApplicationName = String.IsNullOrEmpty(config["applicationName"]) ? System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath : config["applicationName"];
//        }

//        public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyCollection collection)
//        {
//            var userName = (string)context["UserName"];
//            //var isAuthenticated = (bool)context["IsAuthenticated"];

//            var encryption = new EncryptionUtilities();
//            var userDataSource = new AzureTableStorageDataSource();
//            var userEntities = userDataSource.GetUsers(encryption.GetMd5Hash(userName).ToString(), ApplicationName, MergeOption.NoTracking);

//            var userEntity = userEntities.FirstOrDefault();
//            if (userEntity == null) throw new ProviderException("User not found.");

//            // The serializeAs attribute is ignored in this provider implementation.
//            var svc = new SettingsPropertyValueCollection();

//            foreach (SettingsProperty prop in collection)
//            {
//                var pv = new SettingsPropertyValue(prop);

//                switch (prop.Name)
//                {
//                    case PropertyNames.Gender:
//                        pv.PropertyValue = userEntity.Gender;
//                        break;
//                    case PropertyNames.Birthdate:
//                        pv.PropertyValue = userEntity.Birthday;
//                        break;
//                    case PropertyNames.PortraitBlobAddressUri:
//                        pv.PropertyValue = userEntity.PortraitBlobAddressUri;
//                        break;
//                    default:
//                        throw new ProviderException("Unsupported property.");
//                }

//                svc.Add(pv);
//            }

//            return svc;
//        }



//        public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection collection)
//        {
//            // The serializeAs attribute is ignored in this provider implementation.
//            var userName = (string)context["UserName"];

//            var userDataSource = new AzureTableStorageDataSource();
//            var encrypt = new EncryptionUtilities();
//            var userEntities = userDataSource.GetUsers(encrypt.GetMd5Hash(userName).ToString(), ApplicationName);

//            var userEntity = userEntities.FirstOrDefault();
//            if (userEntity == null) throw new ProviderException("User not found.");

//            foreach (SettingsPropertyValue pv in collection)
//            {
//                switch (pv.Property.Name)
//                {
//                    case PropertyNames.Gender:
//                        userEntity.Gender = (int)pv.PropertyValue;
//                        break;
//                    case PropertyNames.Birthdate:
//                        userEntity.Birthday = (DateTime)pv.PropertyValue;
//                        break;
//                    case PropertyNames.PortraitBlobAddressUri:
//                        userEntity.PortraitBlobAddressUri = (string)pv.PropertyValue;
//                        break;
//                    default:
//                        throw new ProviderException("Unsupported property.");
//                }
//            }

//            // Store when profile was updated
//            userEntity.LastProfileUpdatedDate = DateTime.Now;
//            userDataSource.Update(userEntity);
//        }

//        public override string ApplicationName { get; set; }

//        bool DeleteProfile(string userName)
//        {
//            // TODO: Add delete profile code
//            return true;
//        }

//        public override int DeleteProfiles(ProfileInfoCollection profiles)
//        {
//            return profiles.Cast<ProfileInfo>().Count(p => DeleteProfile(p.UserName));
//        }

//        public override int DeleteProfiles(string[] usernames)
//        {
//            return usernames.Count(DeleteProfile);
//        }

//        public override int DeleteInactiveProfiles(ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate)
//        {
//            throw new NotImplementedException();
//        }

//        public override int GetNumberOfInactiveProfiles(ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate)
//        {
//            throw new NotImplementedException();
//        }

//        public override ProfileInfoCollection GetAllProfiles(ProfileAuthenticationOption authenticationOption, int pageIndex, int pageSize, out int totalRecords)
//        {
//            throw new NotImplementedException();
//        }

//        public override ProfileInfoCollection GetAllInactiveProfiles(ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate, int pageIndex, int pageSize, out int totalRecords)
//        {
//            throw new NotImplementedException();
//        }

//        /// <summary>
//        /// Verifies input parameters for page size and page index. 
//        /// Called by GetAllProfiles, GetAllInactiveProfiles, 
//        /// FindProfilesByUserName, and FindInactiveProfilesByUserName.
//        /// </summary>
//        /// <param name="pageIndex"></param>
//        /// <param name="pageSize"></param>
//        void CheckParameters(int pageIndex, int pageSize)
//        {
//            if (pageIndex < 0) throw new ArgumentException("Page index must 0 or greater.");
//            if (pageSize < 1) throw new ArgumentException("Page size must be greater than 0.");
//        }

//        IEnumerable<UserEntity> FindUsersByName(string usernameToMatch, DateTime userInactiveSinceDate, int pageIndex, int pageSize, out int totalRecords)
//        {
//            var userDataSource = new AzureTableStorageDataSource();
//            var userEntities = userDataSource.FindInactiveUsers(usernameToMatch, ApplicationName, userInactiveSinceDate);

//            var users = new List<UserEntity>();

//            try
//            {
//                totalRecords = userEntities.Count();
//            }
//            catch (DataServiceQueryException)
//            {
//                totalRecords = 0;
//            }

//            if (totalRecords == 0) return users;

//            var counter = 0;
//            var startIndex = pageSize * pageIndex;
//            var endIndex = startIndex + pageSize - 1;

//            foreach (var userEntity in userEntities)
//            {
//                if (counter >= startIndex) users.Add(userEntity);

//                if (counter >= endIndex) break;

//                counter++;
//            }

//            return users;
//        }

//        ProfileInfo GetProfileInfoFromUserEnity(UserEntity userEntity)
//        {
//            const bool isAnonymous = false;
//            const int profileInfoSize = 0;

//            // ProfileInfo.Size not currently implemented.
//            return new ProfileInfo(userEntity.Username, isAnonymous, userEntity.LastActivityDate, userEntity.LastProfileUpdatedDate, profileInfoSize);
//        }

//        /// <summary>
//        /// Retrieves a count of profiles and creates a 
//        /// ProfileInfoCollection from the profile data in the 
//        /// storage. 
//        /// </summary>
//        /// <param name="authenticationOption"></param>
//        /// <param name="usernameToMatch"></param>
//        /// <param name="userInactiveSinceDate"></param>
//        /// <param name="pageIndex">Specifying a pageIndex of 0 retrieves a count of the results only.</param>
//        /// <param name="pageSize"></param>
//        /// <param name="totalRecords"></param>
//        /// <returns></returns>
//        ProfileInfoCollection GetProfileInfo(
//            ProfileAuthenticationOption authenticationOption,
//            string usernameToMatch,
//            object userInactiveSinceDate,
//            int pageIndex,
//            int pageSize,
//            out int totalRecords)
//        {
//            if (authenticationOption == ProfileAuthenticationOption.Anonymous)
//                throw new NotImplementedException("ProfileAuthenticationOption.Anonymous");

//            if (userInactiveSinceDate == null) userInactiveSinceDate = DateTime.Now;

//            var users = FindUsersByName(usernameToMatch, (DateTime)userInactiveSinceDate, pageIndex, pageSize, out totalRecords);

//            var profiles = new ProfileInfoCollection();

//            // No profiles found.
//            if (totalRecords <= 0) { return profiles; }

//            // Count profiles only.
//            if (pageSize == 0) { return profiles; }

//            foreach (var userEntity in users)
//                profiles.Add(GetProfileInfoFromUserEnity(userEntity));

//            return profiles;
//        }

//        public override ProfileInfoCollection FindProfilesByUserName(ProfileAuthenticationOption authenticationOption, string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
//        {
//            CheckParameters(pageIndex, pageSize);

//            return GetProfileInfo(authenticationOption, usernameToMatch, null, pageIndex, pageSize, out totalRecords);
//        }

//        public override ProfileInfoCollection FindInactiveProfilesByUserName(ProfileAuthenticationOption authenticationOption, string usernameToMatch, DateTime userInactiveSinceDate, int pageIndex, int pageSize, out int totalRecords)
//        {
//            CheckParameters(pageIndex, pageSize);

//            return GetProfileInfo(authenticationOption, usernameToMatch, userInactiveSinceDate, pageIndex, pageSize, out totalRecords);
//        }
//    }
//}